package com.ashraya.framework.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.springframework.dao.DataAccessException;

/**
 * This will be our data access object framework interface.
 * <p>
 * Created on Feb 28, 2010
 * <p>
 * 
 * @author Pankaj K
 * 
 */
public interface CoreDao<T> {
	/**
	 * Saves the object to the database and returns a serializable object.
	 * 
	 * @param obj
	 * @return
	 * @throws DataAccessException
	 */
	Serializable save(T obj) throws DataAccessException;

	/**
	 * Updates object to the database.
	 * 
	 * @param obj
	 * @throws DataAccessException
	 */
	void update(T obj) throws DataAccessException;

	/**
	 * Save or update all items in the collection
	 * 
	 * @param col
	 * @throws DataAccessException
	 */
	void saveOrUpdate(Collection<T> col) throws DataAccessException;

	/**
	 * Deletes object from the database.
	 * 
	 * @param obj
	 * @throws DataAccessException
	 */
	void delete(T obj) throws DataAccessException;

	/**
	 * Refreshes the values of the object from the database.
	 * 
	 * @param obj
	 */
	void refresh(T obj);

	/**
	 * Evicts the object from the current session. This means that automatic
	 * dirty checking will no longer work.
	 * 
	 * @param obj
	 */
	void evict(T obj);

	/**
	 * Finds and returns an object in the database using class name and Integer
	 * primary key.
	 * 
	 * @param clazz
	 * @param pk
	 * @return
	 * @throws DataAccessException
	 */
	T findByPK(Class<T> clazz, Integer pk) throws DataAccessException;

	/**
	 * Find and return an object by Short primary Key
	 * 
	 * @param clazz
	 * @param pk
	 * @return
	 * @throws DataAccessException
	 */
	T findByPK(Class<T> clazz, Short pk) throws DataAccessException;

	/**
	 * Finds and returns an object in the database using class name and String
	 * primary key.
	 * 
	 * @param clazz
	 * @param pk
	 * @return
	 * @throws DataAccessException
	 */
	T findByPK(Class<T> clazz, String pk) throws DataAccessException;

	/**
	 * Finds and returns a list of objects using class name.
	 * 
	 * @param clazz
	 * @return
	 * @throws DataAccessException
	 */
	List<T> findAll(Class<T> clazz) throws DataAccessException;

	/**
	 * Finds and returns a list of objects using hibernate query string
	 * 
	 * @param hql
	 * @return
	 * @throws DataAccessException
	 */
	List<T> findAll(String hql) throws DataAccessException;

	/**
	 * Find list of objects based on given hql and parameter values
	 * 
	 * @param hql
	 * @param values
	 * @return
	 * @throws DataAccessException
	 */
	List<T> findAll(String hql, Object[] values) throws DataAccessException;

	/**
	 * Find by named params
	 * 
	 * @param hql
	 * @param params
	 * @param values
	 * @return
	 * @throws DataAccessException
	 */
	List<T> findAll(String hql, String[] params, Object[] values) throws DataAccessException;

	/**
	 * Find list of objects based on given HQL and parameter list
	 * 
	 * @param hql
	 * @param values
	 * @return
	 * @throws DataAccessException
	 */
	List<T> findAll(String hql, List<T> values) throws DataAccessException;

	/**
	 * Find list of objects based on an object of type <code>CoreCriteria</code>
	 * 
	 * @param criteria
	 * @return
	 * @throws DataAccessException
	 */
	List<T> findAll(CoreCriteria<T> criteria) throws DataAccessException;

	/**
	 * Finds lists of objects that match given example Uses query by example
	 * 
	 * @param example
	 * @return
	 * @throws DataAccessException
	 */
	List<T> findAll(T example) throws DataAccessException;

	/**
	 * Find by named query
	 * 
	 * @param hql
	 * @return
	 */
	List<T> findByNamedQuery(String hql);

	/**
	 * Find by named query - values as (?)
	 * 
	 * @param hql
	 * @param values
	 * @return
	 */
	List<T> findByNamedQuery(String hql, Object[] values);

	/**
	 * Find by named query - params and values
	 * 
	 * @param hql
	 * @param params
	 * @param values
	 * @return
	 */
	List<T> findByNamedQuery(String hql, String[] params, Object[] values);

	/**
	 * Delete all items
	 * 
	 * @param deleteItems
	 */
	void deleteAll(Collection<T> deleteItems);

	/**
	 * flushes all the outstanding sql statements to the database.
	 */
	void flush();

	public List<T> cachedFindAll(CoreCriteria<T> coreCriteria);

	public List<T> cachedFindAll(String hql, String[] params, Object[] values);

}
